package com.o2o.app.userCenter;

/**
 * 我的求助
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.o2o.app.R;
import com.o2o.app.adapter.MyScrollView;
import com.o2o.app.adapter.MyScrollView.OnScrollListener;
import com.o2o.app.service.ServiceGirlActivity;

public class MineHelpActivity extends Activity implements OnScrollListener {
	public static MineHelpActivity MHA;
	private static final int CLICK_RLT = -1; // 详情
	private static final int CLICK_ZAN = -2; // 点赞

	// item布局按钮
	private ArrayList<RelativeLayout> rltLists = new ArrayList<RelativeLayout>();

	// 点赞布局按钮
	private ArrayList<RelativeLayout> zanLists = new ArrayList<RelativeLayout>();

	// 每项是否可以点赞
	@SuppressLint("UseSparseArrays")
	private HashMap<Integer, Integer> list_zan = new HashMap<Integer, Integer>();

	/**
	 * 返回
	 */
	private Button btn_back;

	/**
	 * 我的求助布局
	 */
	private LinearLayout layout;

	private RelativeLayout rlt_bottom;

	// 加载动画
	private Animation animation1, animation2;

	// 测试数组最大数
	private int i_max = 10;

	/**
	 * 我要求助按钮
	 */
	private RelativeLayout rlt_01;

	/**
	 * 驿站女孩按钮
	 */
	private RelativeLayout rlt_02;

	/**
	 * 定时器
	 */
	private Timer timer;
	private int time = 0;

	private LinearLayout ll_Popup;

	// 保存滚动Y轴值
	private int Yscroll;

	private MyScrollView myScrollView;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				if ((Integer) msg.obj == 0) {
					timer.cancel();
					ll_Popup.setVisibility(View.GONE);
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mine_help);

		showView();

		initViews();
	}

	private void initViews() {
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new ClickEvent());

		rlt_bottom = (RelativeLayout) findViewById(R.id.rlt_bottom);

		animation1 = AnimationUtils.loadAnimation(this,
				R.anim.score_business_query_enter);

		animation2 = AnimationUtils.loadAnimation(this,
				R.anim.score_business_query_enter);

		myScrollView = (MyScrollView) findViewById(R.id.scrollView);
		myScrollView.setOnScrollListener(this);

		rlt_01 = (RelativeLayout) findViewById(R.id.rlt_01);
		rlt_01.setOnClickListener(new ClickEvent());

		rlt_02 = (RelativeLayout) findViewById(R.id.rlt_02);
		rlt_02.setOnClickListener(new ClickEvent());

	}

	/**
	 * 显示已发布
	 */
	private void showView() {
		layout = (LinearLayout) findViewById(R.id.lly_all_content01);
		// layout.removeAllViews();
		layout.setVisibility(View.VISIBLE);

		for (int i = 0; i < i_max; i++) {
			LinearLayout child = (LinearLayout) LayoutInflater.from(
					MineHelpActivity.this)
					.inflate(R.layout.mine_help_lly, null);
			child.setId(i);

			RelativeLayout rlt_demo = (RelativeLayout) child
					.findViewById(R.id.rlt_demo);
			rlt_demo.setId(CLICK_ZAN);
			rlt_demo.setOnClickListener(new ClickEvent());
			zanLists.add(rlt_demo);

			RelativeLayout rlt_item = (RelativeLayout) child
					.findViewById(R.id.rlt_01);
			rlt_item.setId(CLICK_RLT);
			rlt_item.setOnClickListener(new ClickEvent());
			rltLists.add(rlt_item);

			layout.addView(child);

			list_zan.put(i, 0);
		}
	}

	/**
	 * 点击事件
	 * 
	 * @author Administrator
	 * 
	 */
	class ClickEvent implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_back: { // 返回
				finish();
			}
				break;

			case CLICK_RLT: {
				checkRltItem(v);
			}
				break;

			case CLICK_ZAN: {
				checkzanItem(v);
			}
				break;
			case R.id.rlt_01: { // 求助

			}
				break;
			case R.id.rlt_02: { // 驿站女孩
				Intent intent = new Intent(MineHelpActivity.this,
						ServiceGirlActivity.class);
				startActivity(intent);
			}
				break;
			default:
				break;
			}
		}

	}

	/**
	 * 点击布局
	 */
	private void checkRltItem(View v) {
		int index = -1;
		for (int i = 0; i < i_max; i++) { // 遍历选择了哪项
			if (rltLists.get(i).equals(v)) { // 为真、确定是选了哪项
				index = i;
			}
		}

		if (index >= 0) {
			Intent intent = new Intent(MineHelpActivity.this,
					MineHelpItemActivity.class);
			startActivity(intent);
		}
	}

	/**
	 * 点赞
	 */
	private void checkzanItem(View v) {
		int index = -1;
		for (int i = 0; i < i_max; i++) { // 遍历选择了哪项
			if (zanLists.get(i).equals(v)) { // 为真、确定是选了哪项
				index = i;
			}
		}

		if (index >= 0 && list_zan.get(index) == 0) { // 点赞 +1
			LinearLayout layout = (LinearLayout) findViewById(index);

			ll_Popup = (LinearLayout) layout.findViewById(R.id.ll_popupLayout);

			ll_Popup.setVisibility(View.VISIBLE); // 显示布局
			ll_Popup.startAnimation(animation1); // 开始动画

			ImageView iv_ding = (ImageView) layout.findViewById(R.id.iv_ding);

			TextView tv_dingnum = (TextView) layout
					.findViewById(R.id.tv_dingnum);

			tv_dingnum.setText((Integer.parseInt(tv_dingnum.getText()
					.toString()) + 1) + "");

			iv_ding.setBackgroundResource(R.drawable.ding_2);
			tv_dingnum.setTextColor(getResources().getColor(R.color.red));

			runTimerTask();

			// 点赞后，不能继续在点
			list_zan.put(index, 1);

		}
	}

	/**
	 * 执行定时任务
	 */
	private void runTimerTask() {
		timer = new Timer();
		time = 1;
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 1;
				msg.obj = time;
				handler.sendMessage(msg);
				time--;
			}
		};
		timer.schedule(task, 0, 1000);
	}

	@Override
	public void onScroll(int scrollY) {
		// TODO Auto-generated method stub

		if (scrollY == 0) {
			rlt_bottom.setVisibility(View.VISIBLE);
			Yscroll = scrollY;
		} else {
			if (Yscroll < scrollY) { // 往下滚动
				rlt_bottom.setVisibility(View.GONE);
				Yscroll = scrollY;
			} else if (Yscroll > scrollY) { // 向上滚动
				rlt_bottom.setVisibility(View.VISIBLE);
				Yscroll = scrollY;
			} else { // 相同
				// rlt_bottom.setVisibility(View.GONE);
				// Yscroll = scrollY;
			}
		}
	}
}
